package com.chenshu.test;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-03-26
 * Time：22:58
 */
public class ReflectionUtils {
    public static void main(String[] args) throws ClassNotFoundException {
        api_01();
    }

    public static void api_01() throws ClassNotFoundException {
        Class<?> userCls = Class.forName("com.chenshu.test.User");
        System.out.println(userCls.getName());

        System.out.println(userCls.getSimpleName());

        Field[] fields = userCls.getFields();
        for (Field field : fields) {
            System.out.println("本类及父类的public属性：" + field.getName());
        }

        Field[] declareFields = userCls.getDeclaredFields();
        for (Field field : declareFields) {
            System.out.println("本类的所有属性：" + field.getName());
        }

        Method[] methods = userCls.getMethods();
        for (Method method : methods) {
            System.out.println("本类及父类的public方法：" + method.getName());
        }

        Method[] declareMethods = userCls.getDeclaredMethods();
        for (Method method : declareMethods) {
            System.out.println("本类的所有方法：" + method.getName());
        }

        Constructor<?>[] constructors = userCls.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("本类的public构造方法：" + constructor.getName());
        }

        Constructor<?>[] declaredConstructors = userCls.getDeclaredConstructors();
        for (Constructor constructor : declaredConstructors) {
            System.out.println("本类的所有构造方法：" + constructor.getName());
        }

        System.out.println(userCls.getPackage());

        Class<?> superclass = userCls.getSuperclass();
        System.out.println(superclass);

        Class<?>[] interfaces = userCls.getInterfaces();
        for(Class anInterface : interfaces) {
            System.out.println("接口信息：" + anInterface);
        }

        Annotation[] annotations = userCls.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("注解信息：" + annotation);
        }
    }
}

class People {

}

interface IA {

}
interface IB{

}

@Resource
class User extends People implements IA, IB{
    //四种修饰符修饰的属性
    public String name;
    protected int age;
    String gender;
    private double salary;

    //无参公开构造器
    public User() {

    }
    //有参公开构造器
    public User(String name) {
        this.name = name;
    }
    //有参私有构造器
    private User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //四种修饰符的方法
    public void m1() {

    }
    protected void m2() {

    }
    void m3() {

    }
    private void m4() {

    }
}
